﻿// ZFTDlg.cpp: 实现文件
//

#include "pch.h"
#include "3180101584SX1.h"
#include "ZFTDlg.h"
#include "afxdialogex.h"

extern OpenImageClass OImageC;
// ZFTDlg 对话框

IMPLEMENT_DYNAMIC(ZFTDlg, CDialog)

ZFTDlg::ZFTDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DIALOG_ZFT, pParent)
{

}

ZFTDlg::~ZFTDlg()
{
}

void ZFTDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(ZFTDlg, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_LAYOUT, &ZFTDlg::OnBnClickedButtonLayout)
	ON_WM_LBUTTONUP()
	ON_BN_CLICKED(IDC_BUTTON_ADAPT, &ZFTDlg::OnBnClickedButtonAdapt)
END_MESSAGE_MAP()


// ZFTDlg 消息处理程序


BOOL ZFTDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化

	pDC = this->GetDC();

	OldLineFlagR = FALSE;
	OldLineFlagG = FALSE;
	OldLineFlagB = FALSE;

	SetDlgItemText(IDC_EDIT_LEFT, "2");
	SetDlgItemText(IDC_EDIT_RIGHT, "2");

	CWnd* pWnd = AfxGetMainWnd();     //获取主窗口指针

	CDC * pDC = pWnd->GetDC();
	hdc = pDC->GetSafeHdc();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void ZFTDlg::DrawRFact()
{
	CRect rect;
	GetDlgItem(IDC_STATIC_RIMG)->GetClientRect(rect);
	//ClientToScreen(&rect

	pDC = this->GetDlgItem(IDC_STATIC_RIMG)->GetDC();
	
	pDC->SetROP2(R2_COPYPEN);

	float fStepX = float(rect.Width() - 2) / float(256);
	float fStepY = float(rect.Height() - 2) / (OImageC.etmImg->Rmax * float(1.05));
	int iPenWidth = (int)fStepX / 1 + 1;
	CPen *pPen9 = new CPen;

	CPen *pOldPn9;

	pPen9->CreatePen(PS_SOLID, iPenWidth, RGB(255, 150, 150));

	pOldPn9 = pDC->SelectObject(pPen9);
	for (int i = 0; i < 256; i++)
	{
		pDC->MoveTo(int(i * fStepX + 1), int(rect.Height() - 1));
		pDC->LineTo(int(i * fStepX + 1), int(rect.Height() - 1 - fStepY * OImageC.etmImg->factR[i]));
	}
}

void ZFTDlg::DrawGFact()
{
	CRect rect;
	GetDlgItem(IDC_STATIC_GIMG)->GetClientRect(rect);
	//ClientToScreen(&rect

	pDC = this->GetDlgItem(IDC_STATIC_GIMG)->GetDC();

	pDC->SetROP2(R2_COPYPEN);

	float fStepX = float(rect.Width() - 2) / float(256);
	float fStepY = float(rect.Height() - 2) / (OImageC.etmImg->Gmax * float(1.05));
	int iPenWidth = (int)fStepX / 1 + 1;
	CPen *pPen9 = new CPen;

	CPen *pOldPn9;

	pPen9->CreatePen(PS_SOLID, iPenWidth, RGB(150, 255, 150));

	pOldPn9 = pDC->SelectObject(pPen9);
	for (int i = 0; i < 256; i++)
	{
		pDC->MoveTo(int(i * fStepX + 1), int(rect.Height() - 1));
		pDC->LineTo(int(i * fStepX + 1), int(rect.Height() - 1 - fStepY * OImageC.etmImg->factG[i]));
	}
}

void ZFTDlg::DrawBFact()
{
	CRect rect;
	GetDlgItem(IDC_STATIC_BIMG)->GetClientRect(rect);
	//ClientToScreen(&rect

	pDC = this->GetDlgItem(IDC_STATIC_BIMG)->GetDC();

	pDC->SetROP2(R2_COPYPEN);

	float fStepX = float(rect.Width() - 2) / float(256);
	float fStepY = float(rect.Height() - 2) / (OImageC.etmImg->Bmax * float(1.05));
	int iPenWidth = (int)fStepX / 1 + 1;
	CPen *pPen9 = new CPen;

	CPen *pOldPn9;

	pPen9->CreatePen(PS_SOLID, iPenWidth, RGB(150, 150, 255));

	pOldPn9 = pDC->SelectObject(pPen9);
	for (int i = 0; i < 256; i++)
	{
		pDC->MoveTo(int(i * fStepX + 1), int(rect.Height() - 1));
		pDC->LineTo(int(i * fStepX + 1), int(rect.Height() - 1 - fStepY * OImageC.etmImg->factB[i]));
	}
}

void ZFTDlg::DrawRLine()
{
	CRect rect;
	GetDlgItem(IDC_STATIC_RIMG)->GetClientRect(rect);
	//ClientToScreen(&rect

	pDC = this->GetDlgItem(IDC_STATIC_RIMG)->GetDC();

	pDC->SetROP2(R2_NOTXORPEN);

	CPen * cPen3 = new CPen;//生明画笔
	cPen3->CreatePen(PS_SOLID, 1, RGB(150, 0, 0));
	pDC->SelectObject(cPen3);

	float fStepX = float(rect.Width() - 2) / float(256);

	pDC->MoveTo(OImageC.etmImg->EdRmin * fStepX, int(rect.Height() - 1));
	pDC->LineTo(OImageC.etmImg->EdRmin * fStepX, 1);

	pDC->MoveTo(OImageC.etmImg->EdRmax * fStepX, int(rect.Height() - 1));
	pDC->LineTo(OImageC.etmImg->EdRmax * fStepX, 1);

	OldLineFlagR = TRUE;
}

void ZFTDlg::DrawGLine()
{
	CRect rect;
	GetDlgItem(IDC_STATIC_GIMG)->GetClientRect(rect);
	//ClientToScreen(&rect

	pDC = this->GetDlgItem(IDC_STATIC_GIMG)->GetDC();

	pDC->SetROP2(R2_NOTXORPEN);

	CPen * cPen3 = new CPen;//生明画笔
	cPen3->CreatePen(PS_SOLID, 1, RGB(0, 150, 0));
	pDC->SelectObject(cPen3);

	float fStepX = float(rect.Width() - 2) / float(256);

	pDC->MoveTo(OImageC.etmImg->EdGmin * fStepX, int(rect.Height() - 1));
	pDC->LineTo(OImageC.etmImg->EdGmin * fStepX, 1);

	pDC->MoveTo(OImageC.etmImg->EdGmax * fStepX, int(rect.Height() - 1));
	pDC->LineTo(OImageC.etmImg->EdGmax * fStepX, 1);

	OldLineFlagG = TRUE;
}

void ZFTDlg::DrawBLine()
{
	CRect rect;
	GetDlgItem(IDC_STATIC_BIMG)->GetClientRect(rect);
	//ClientToScreen(&rect

	pDC = this->GetDlgItem(IDC_STATIC_BIMG)->GetDC();

	pDC->SetROP2(R2_NOTXORPEN);

	CPen * cPen3 = new CPen;//生明画笔
	cPen3->CreatePen(PS_SOLID, 1, RGB(0, 0, 150));
	pDC->SelectObject(cPen3);

	float fStepX = float(rect.Width() - 2) / float(256);

	pDC->MoveTo(OImageC.etmImg->EdBmin * fStepX, int(rect.Height() - 1));
	pDC->LineTo(OImageC.etmImg->EdBmin * fStepX, 1);

	pDC->MoveTo(OImageC.etmImg->EdBmax * fStepX, int(rect.Height() - 1));
	pDC->LineTo(OImageC.etmImg->EdBmax * fStepX, 1);

	OldLineFlagB = TRUE;
}

void ZFTDlg::DrawOldRLine()
{
	if (OldLineFlagR == TRUE)
	{
		DrawRLine();
		OldLineFlagR = FALSE;
	}
}

void ZFTDlg::DrawOldGLine()
{
	if (OldLineFlagG == TRUE)
	{
		DrawGLine();
		OldLineFlagG = FALSE;
	}
}

void ZFTDlg::DrawOldBLine()
{
	if (OldLineFlagB == TRUE)
	{
		DrawBLine();
		OldLineFlagB = FALSE;
	}
}


void ZFTDlg::OnBnClickedButtonLayout()
{
	DrawRFact();
	DrawGFact();
	DrawBFact();

	CEdit* pBoxOne;
	CString output;
	output.Format("%d", OImageC.etmImg->EdRmin);
	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_RMIN);
	//赋值
	pBoxOne-> SetWindowText(output);

	output.Format("%d", OImageC.etmImg->EdRmax);
	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_RMAX);
	//赋值
	pBoxOne->SetWindowText(output);

	output.Format("%d", OImageC.etmImg->EdGmin);
	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_GMIN);
	//赋值
	pBoxOne->SetWindowText(output);

	output.Format("%d", OImageC.etmImg->EdGmax);
	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_GMAX);
	//赋值
	pBoxOne->SetWindowText(output);

	output.Format("%d", OImageC.etmImg->EdBmin);
	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_BMIN);
	//赋值
	pBoxOne->SetWindowText(output);

	output.Format("%d", OImageC.etmImg->EdBmax);
	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_BMAX);
	//赋值
	pBoxOne->SetWindowText(output);

	DrawRLine();
	DrawGLine();
	DrawBLine();
}


void ZFTDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	int flag = 0;
	CRect  rect;
	GetClientRect(&rect);//获取客户区的大小
	CPoint temppoint;
	GetCursorPos(&temppoint);//获取当前指针的坐标（注意，这是屏幕的）
	GetWindowRect(&rect);//获取客户区（客户区的左上角）相对于屏幕的位置
	int x = (temppoint.x);//通过变换的到客户区的坐标
	int y = (temppoint.y);

	CRect rectR;
	GetDlgItem(IDC_STATIC_RIMG)->GetWindowRect(rectR);

	CRect rectG;
	GetDlgItem(IDC_STATIC_GIMG)->GetWindowRect(rectG);

	CRect rectB;
	GetDlgItem(IDC_STATIC_BIMG)->GetWindowRect(rectB);

	if (x > rectR.left && x < rectR.right)
	{
		if (y < rectR.bottom && y > rectR.top)
		{
			flag = 1;
			DrawOldRLine();

			int tempnum = x - rectR.left;
			int allnum = rectR.right - rectR.left;

			tempnum = (int)(tempnum / (float)allnum * 256);

			if (tempnum <= OImageC.etmImg->EdRmin)
			{
				OImageC.etmImg->EdRmin = tempnum;
			}
			else if (tempnum >= OImageC.etmImg->EdRmax)
			{
				OImageC.etmImg->EdRmax = tempnum;
			}
			else if (tempnum - OImageC.etmImg->EdRmin < OImageC.etmImg->EdRmax - tempnum)
			{
				OImageC.etmImg->EdRmin = tempnum;
			}
			else if (tempnum - OImageC.etmImg->EdRmin >= OImageC.etmImg->EdRmax - tempnum)
			{
				OImageC.etmImg->EdRmax = tempnum;
			}

			DrawRLine();

			CEdit* pBoxOne;
			CString output;
			output.Format("%d", OImageC.etmImg->EdRmin);
			pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_RMIN);
			//赋值
			pBoxOne->SetWindowText(output);

			output.Format("%d", OImageC.etmImg->EdRmax);
			pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_RMAX);
			//赋值
			pBoxOne->SetWindowText(output);

			OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
		}
		else if (y < rectG.bottom && y > rectG.top)
		{
			flag = 2;
			DrawOldGLine();

			int tempnum = x - rectG.left;
			int allnum = rectG.right - rectG.left;

			tempnum = (int)(tempnum / (float)allnum * 256);

			if (tempnum <= OImageC.etmImg->EdGmin)
			{
				OImageC.etmImg->EdGmin = tempnum;
			}
			else if (tempnum >= OImageC.etmImg->EdGmax)
			{
				OImageC.etmImg->EdGmax = tempnum;
			}
			else if (tempnum - OImageC.etmImg->EdGmin < OImageC.etmImg->EdGmax - tempnum)
			{
				OImageC.etmImg->EdGmin = tempnum;
			}
			else if (tempnum - OImageC.etmImg->EdGmin >= OImageC.etmImg->EdGmax - tempnum)
			{
				OImageC.etmImg->EdGmax = tempnum;
			}

			DrawGLine();

			CEdit* pBoxOne;
			CString output;
			output.Format("%d", OImageC.etmImg->EdGmin);
			pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_GMIN);
			//赋值
			pBoxOne->SetWindowText(output);

			output.Format("%d", OImageC.etmImg->EdGmax);
			pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_GMAX);
			//赋值
			pBoxOne->SetWindowText(output);

			OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
		}
		else if (y < rectB.bottom && y > rectB.top)
		{
			flag = 3;
			DrawOldBLine();

			int tempnum = x - rectB.left;
			int allnum = rectB.right - rectB.left;

			tempnum = (int)(tempnum / (float)allnum * 256);

			if (tempnum <= OImageC.etmImg->EdBmin)
			{
				OImageC.etmImg->EdBmin = tempnum;
			}
			else if (tempnum >= OImageC.etmImg->EdBmax)
			{
				OImageC.etmImg->EdBmax = tempnum;
			}
			else if (tempnum - OImageC.etmImg->EdRmin < OImageC.etmImg->EdBmax - tempnum)
			{
				OImageC.etmImg->EdBmin = tempnum;
			}
			else if (tempnum - OImageC.etmImg->EdRmin >= OImageC.etmImg->EdBmax - tempnum)
			{
				OImageC.etmImg->EdBmax = tempnum;
			}

			DrawBLine();

			CEdit* pBoxOne;
			CString output;
			output.Format("%d", OImageC.etmImg->EdBmin);
			pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_BMIN);
			//赋值
			pBoxOne->SetWindowText(output);

			output.Format("%d", OImageC.etmImg->EdBmax);
			pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_BMAX);
			//赋值
			pBoxOne->SetWindowText(output);

			OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);
		}
	}

	CDialog::OnLButtonUp(nFlags, point);
}


void ZFTDlg::OnBnClickedButtonAdapt()
{
	DrawRLine();
	DrawGLine();
	DrawBLine();

	CString GetText;
	GetDlgItemText(IDC_EDIT_LEFT, GetText);
	int iLeft = _ttoi(GetText);

	GetDlgItemText(IDC_EDIT_RIGHT, GetText);
	int iRight = _ttoi(GetText);

	double AllFactR = 0;
	double AllFactG = 0;
	double AllFactB = 0;

	for (int ii = 0; ii < 256; ii++)
	{
		AllFactR = AllFactR + OImageC.etmImg->factR[ii];
		AllFactG = AllFactG + OImageC.etmImg->factG[ii];
		AllFactB = AllFactB + OImageC.etmImg->factB[ii];
	}

	double FactRLeft = AllFactR / 100 * iLeft;
	double FactRRight = AllFactR / 100 * (100 - iRight);

	double FactGLeft = AllFactG / 100 * iLeft;
	double FactGRight = AllFactG / 100 * (100 - iRight);

	double FactBLeft = AllFactB / 100 * iLeft;
	double FactBRight = AllFactB / 100 * (100 - iRight);

	int TempR = 0;
	int FinalRLeft = 0;
	int FinalRRight = 0;

	int TempG = 0;
	int FinalGLeft = 0;
	int FinalGRight = 0;

	int TempB = 0;
	int FinalBLeft = 0;
	int FinalBRight = 0;

	for (int ii = 0; ii < 256; ii++)
	{
		TempR = TempR + OImageC.etmImg->factR[ii];
		if (TempR > FactRLeft)
		{
			FinalRLeft = ii;
			break;
		}
	}

	TempR = AllFactR;
	for (int ii = 255; ii >= 0; ii--)
	{
		TempR = TempR - OImageC.etmImg->factR[ii];
		if (TempR < FactRRight)
		{
			FinalRRight = ii;
			break;
		}
	}

	for (int ii = 0; ii < 256; ii++)
	{
		TempG = TempG + OImageC.etmImg->factG[ii];
		if (TempG > FactGLeft)
		{
			FinalGLeft = ii;
			break;
		}
	}

	TempG = AllFactG;
	for (int ii = 255; ii >= 0; ii--)
	{
		TempG = TempG - OImageC.etmImg->factG[ii];
		if (TempG < FactGRight)
		{
			FinalGRight = ii;
			break;
		}
	}

	for (int ii = 0; ii < 256; ii++)
	{
		TempB = TempB + OImageC.etmImg->factB[ii];
		if (TempB > FactBLeft)
		{
			FinalBLeft = ii;
			break;
		}
	}

	TempB = AllFactB;
	for (int ii = 255; ii >= 0; ii--)
	{
		TempB = TempB - OImageC.etmImg->factB[ii];
		if (TempB < FactBRight)
		{
			FinalBRight = ii;
			break;
		}
	}

	OImageC.etmImg->EdRmax = FinalRRight;
	OImageC.etmImg->EdRmin = FinalRLeft;

	OImageC.etmImg->EdGmax = FinalGRight;
	OImageC.etmImg->EdGmin = FinalGLeft;

	OImageC.etmImg->EdBmax = FinalBRight;
	OImageC.etmImg->EdBmin = FinalBLeft;

	DrawRLine();
	DrawGLine();
	DrawBLine();

	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);

	CString SetText;

	SetText.Format("%d", OImageC.etmImg->EdRmin);
	SetDlgItemText(IDC_EDIT_RMIN, SetText);
	SetText.Format("%d", OImageC.etmImg->EdRmax);
	SetDlgItemText(IDC_EDIT_RMAX, SetText);

	SetText.Format("%d", OImageC.etmImg->EdGmin);
	SetDlgItemText(IDC_EDIT_GMIN, SetText);
	SetText.Format("%d", OImageC.etmImg->EdGmax);
	SetDlgItemText(IDC_EDIT_GMAX, SetText);

	SetText.Format("%d", OImageC.etmImg->EdBmin);
	SetDlgItemText(IDC_EDIT_BMIN, SetText);
	SetText.Format("%d", OImageC.etmImg->EdBmax);
	SetDlgItemText(IDC_EDIT_BMAX, SetText);
}
